home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6246 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: interramp.com!usenet
  2. From: Barnett@interramp.com (Barnett E. Kurtz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: cin.get(string) problem
  5. Date: Mon, 12 Feb 1996 02:14:40 GMT
  6. Organization: EntroData, Inc.
  7. Message-ID: <4fm7ti$ls1@usenet6.interramp.com>
  8. References: <4fk02a$nh@reader2.ix.netcom.com>
  9. Reply-To: Barnett@interramp.com
  10. NNTP-Posting-Host: ip167.philadelphia.pa.interramp.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Hi Kurt,
  14.  
  15. You probably should use cin.getline rather than cin.get for that kind
  16. of task. cin.get leaves the delimiter behind in the input buffer
  17. (default is '\n') so the next call to cin.get returns a zero length
  18. string. If you really need to use cin.get, then follow with a int
  19. dummy = cin.get(); This will cause the leftover delimiter to be
  20. extracted from the buffer so that you can follow with another
  21. cin.get(string,sizeof(string)); 
  22.  
  23. BTW as far as I know this is the standard behavior for cin.get. It
  24. works the same way in MS VC++ and BCC++ 4.5, DEC C++, etc.
  25.  
  26. greinerk@ix.netcom.com (Kurt W. Greiner) wrote:
  27.  
  28. >Hi all,
  29. >        i was trying some really simple io for a program that just gets 
  30. >values, here is a sample of code
  31.  
  32. >#include <iostream.h>
  33.  
  34. >void main(void)
  35. >{
  36. >        char string[80];
  37. >        cin.get(string,sizeof(string));
  38. >        cin.get(string,sizeof(string));
  39. >}
  40.  
  41. >ok, say i want to read in a name on the first cin, i type in "john smith" and 
  42. >the prompt, john gets thrown in to the first and smith the second, i do not 
  43. >even get prompted for the second cin. it does not happen if i type in a string 
  44. >with out spaces. i am using borland c++ 2.0, am i doing something wrong or is 
  45. >there an other way of doing this other than gets(string)?
  46.  
  47. >kurt
  48.  
  49. -
  50. barnett@interramp.com
  51. -
  52.  
  53.